<template>
  <div class="history-container">
    <h2>预测历史记录</h2>
    
    <div v-if="loading" class="loading">
      加载中...
    </div>
    
    <div v-else-if="history.length === 0" class="empty-state">
      暂无预测记录
    </div>
    
    <div v-else class="history-list">
      <div v-for="item in history" :key="item.id" class="history-item">
        <div class="history-image">
          <img :src="getImageUrl(item.image_path)" :alt="item.prediction_label" />
        </div>
        
        <div class="history-details">
          <div class="prediction-info">
            <h3>{{ item.prediction_label }}</h3>
            <div class="confidence">
              置信度：{{ (item.confidence * 100).toFixed(2) }}%
            </div>
            <div class="prediction-details">
              <div class="detail-item">
                <span class="label">识别时间：</span>
                <span class="value">{{ formatTime(item.prediction_time) }}</span>
              </div>
              <div class="detail-item">
                <span class="label">识别用户：</span>
                <span class="value">{{ item.username }}</span>
              </div>
              <div class="detail-item">
                <span class="label">图片路径：</span>
                <span class="value">{{ item.image_path }}</span>
              </div>
              <div class="detail-item">
                <span class="label">记录ID：</span>
                <span class="value">{{ item.id }}</span>
              </div>
            </div>
          </div>
          
          <div class="actions">
            <button 
              class="delete-btn"
              @click="deleteHistory(item.id)"
              :disabled="isDeleting === item.id"
            >
              {{ isDeleting === item.id ? '删除中...' : '删除' }}
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import api from '../api/config'

const history = ref([])
const loading = ref(true)
const isDeleting = ref(null)

// 获取历史记录
const fetchHistory = async () => {
  loading.value = true
  try {
    const response = await api.get('/api/prediction-history')
    history.value = response.data.history
  } catch (error) {
    console.error('获取历史记录失败:', error)
    alert('获取历史记录失败，请重试')
  } finally {
    loading.value = false
  }
}

// 删除历史记录
const deleteHistory = async (id) => {
  if (!confirm('确定要删除这条记录吗？')) return
  
  isDeleting.value = id
  try {
    await api.delete(`/api/prediction-history/${id}`)
    history.value = history.value.filter(item => item.id !== id)
    alert('记录已删除')
  } catch (error) {
    console.error('删除记录失败:', error)
    alert('删除记录失败，请重试')
  } finally {
    isDeleting.value = null
  }
}

// 格式化时间
const formatTime = (timeStr) => {
  const date = new Date(timeStr)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit',
    hour12: false
  })
}

// 获取图片URL
const getImageUrl = (path) => {
  // 从完整路径中提取文件名
  const filename = path.split('/').pop()
  return `http://172.31.53.239:5000/uploads/${filename}`
}

onMounted(() => {
  fetchHistory()
})
</script>

<style scoped>
.history-container {
  padding: 2rem;
  max-width: 1200px;
  margin: 0 auto;
}

.loading {
  text-align: center;
  padding: 2rem;
  color: #666;
}

.empty-state {
  text-align: center;
  padding: 3rem;
  color: #666;
  background: #f8f9fa;
  border-radius: 8px;
}

.history-list {
  display: grid;
  gap: 1.5rem;
}

.history-item {
  display: flex;
  gap: 1.5rem;
  background: white;
  padding: 1.5rem;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.history-image {
  flex: 0 0 200px;
}

.history-image img {
  width: 100%;
  height: 150px;
  object-fit: cover;
  border-radius: 4px;
}

.history-details {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.prediction-info h3 {
  margin: 0 0 0.5rem 0;
  color: #2c3e50;
}

.confidence {
  color: #42b983;
  font-weight: bold;
  margin-bottom: 1rem;
}

.prediction-details {
  background: #f8f9fa;
  padding: 1rem;
  border-radius: 4px;
  margin-top: 1rem;
}

.detail-item {
  display: flex;
  margin-bottom: 0.5rem;
  font-size: 0.9rem;
}

.detail-item:last-child {
  margin-bottom: 0;
}

.detail-item .label {
  color: #666;
  width: 80px;
  flex-shrink: 0;
}

.detail-item .value {
  color: #2c3e50;
  word-break: break-all;
}

.actions {
  margin-top: 1rem;
  display: flex;
  justify-content: flex-end;
}

.delete-btn {
  padding: 0.5rem 1rem;
  background-color: #dc3545;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.delete-btn:hover {
  background-color: #c82333;
}

.delete-btn:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}
</style> 